package org.ens.delegation.dao.impl;

import java.util.List;
import org.ens.delegation.bo.TypeEtablissement;
import org.ens.delegation.dao.ITypeEtablissementDao;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class TypeEtablissementDao extends GenericDao<TypeEtablissement, String> implements ITypeEtablissementDao{

	@Override
	public Class<TypeEtablissement> getReferencedClass() {
		return TypeEtablissement.class;
	}

        
    @Override
    public List<TypeEtablissement> findByFilters(String orderCol, String orderDir, String description) {
                
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(description != null && !description.isEmpty())
            criteria.add(Restrictions.like("description", description));
                
        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public List<TypeEtablissement> findByFiltersWithFirstMaxResult(int first, int max, String orderCol, String orderDir, String description) {
                        
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(description != null && !description.isEmpty())
            criteria.add(Restrictions.like("description", description));
        
        criteria.setFirstResult(first);
        criteria.setMaxResults(max);
        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public Integer getRowCountByFilters(String description) {
                        
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
                
        if(description != null && !description.isEmpty())
            criteria.add(Restrictions.like("description", description));
                
        return (Integer) criteria.uniqueResult();
    }
}
